package GUI;

import Dominio.Persona;
import Logica.PuenteConexion;
import java.awt.Image;
import java.io.IOException;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author Fracisco
 */

public class Pagina_Creacion_PersonalW extends javax.swing.JFrame {

    /**
     * Creates new form _PersonalW
     */
    private JFileChooser fc = new JFileChooser();
    private String direccion_foto = null;
    
    public Pagina_Creacion_PersonalW() 
    {
        initComponents();
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLayeredPane3 = new javax.swing.JLayeredPane();
        jLabel12 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTNombre_1 = new javax.swing.JTextField();
        jTApellido_1 = new javax.swing.JTextField();
        jTDiscapacidad = new javax.swing.JTextField();
        jLayeredPane4 = new javax.swing.JLayeredPane();
        jLabel10 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jTTelefono_c = new javax.swing.JTextField();
        jTTelefono_h = new javax.swing.JTextField();
        jTCorreo = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTDireccion = new javax.swing.JTextArea();
        jLabel8 = new javax.swing.JLabel();
        jCEstado_Civil = new javax.swing.JComboBox();
        jRFemenino = new javax.swing.JRadioButton();
        jRMasculino = new javax.swing.JRadioButton();
        jLayeredPane2 = new javax.swing.JLayeredPane();
        jDFecha_Nac = new com.toedter.calendar.JDateChooser();
        jLayeredPane6 = new javax.swing.JLayeredPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jLFoto = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTNombre_2 = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jTApellido_2 = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jTci = new javax.swing.JTextField();
        jBRegresar = new javax.swing.JButton();
        jBAceptar = new javax.swing.JButton();
        jLayeredPane5 = new javax.swing.JLayeredPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setBounds(0, 0, 0, 120);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setText("jLabel2");
        jLabel2.setBounds(110, 0, 980, 120);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane3.setBorder(javax.swing.BorderFactory.createTitledBorder("Informacion General del Empleado"));

        jLabel12.setText("Discapacidad:");
        jLabel12.setBounds(50, 390, 66, 20);
        jLayeredPane3.add(jLabel12, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel11.setText("Estado Civil:");
        jLabel11.setBounds(50, 190, 70, 20);
        jLayeredPane3.add(jLabel11, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel7.setText("Sexo:");
        jLabel7.setBounds(80, 160, 28, 20);
        jLayeredPane3.add(jLabel7, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel6.setText(" Fecha de Nacimiento:");
        jLabel6.setBounds(10, 130, 110, 20);
        jLayeredPane3.add(jLabel6, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel5.setText("Primer apellido:");
        jLabel5.setBounds(10, 100, 100, 20);
        jLayeredPane3.add(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel4.setText("Primer nombre:");
        jLabel4.setBounds(10, 70, 80, 20);
        jLayeredPane3.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jTNombre_1.setBounds(140, 70, 110, 20);
        jLayeredPane3.add(jTNombre_1, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jTApellido_1.setBounds(140, 100, 110, 20);
        jLayeredPane3.add(jTApellido_1, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jTDiscapacidad.setBounds(120, 390, 180, 20);
        jLayeredPane3.add(jTDiscapacidad, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane4.setBorder(javax.swing.BorderFactory.createTitledBorder("Informacion de Contacto"));

        jLabel10.setText("Tlf  Hogar:");
        jLabel10.setBounds(50, 130, 60, 20);
        jLayeredPane4.add(jLabel10, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel9.setText("Tlf Celular:");
        jLabel9.setBounds(50, 100, 60, 20);
        jLayeredPane4.add(jLabel9, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jTTelefono_c.setBounds(110, 100, 80, 20);
        jLayeredPane4.add(jTTelefono_c, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jTTelefono_h.setBounds(110, 130, 80, 20);
        jLayeredPane4.add(jTTelefono_h, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jTCorreo.setBounds(110, 70, 180, 20);
        jLayeredPane4.add(jTCorreo, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel14.setText("Correo Electronico:");
        jLabel14.setBounds(10, 70, 100, 20);
        jLayeredPane4.add(jLabel14, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTDireccion.setColumns(20);
        jTDireccion.setRows(5);
        jScrollPane1.setViewportView(jTDireccion);

        jScrollPane1.setBounds(110, 20, 350, 40);
        jLayeredPane4.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel8.setText("Direccion:");
        jLabel8.setBounds(50, 20, 50, 20);
        jLayeredPane4.add(jLabel8, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane4.setBounds(10, 220, 480, 160);
        jLayeredPane3.add(jLayeredPane4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jCEstado_Civil.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Soltero/a", "Casado/a", "Divorciado/a", "Viudo/a" }));
        jCEstado_Civil.setBounds(120, 190, 90, 20);
        jLayeredPane3.add(jCEstado_Civil, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jRFemenino.setText("Femenino");
        jRFemenino.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRFemeninoActionPerformed(evt);
            }
        });
        jRFemenino.setBounds(190, 160, 71, 20);
        jLayeredPane3.add(jRFemenino, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jRMasculino.setText("Masculino");
        jRMasculino.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRMasculinoActionPerformed(evt);
            }
        });
        jRMasculino.setBounds(120, 160, 71, 20);
        jLayeredPane3.add(jRMasculino, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane2.setBorder(javax.swing.BorderFactory.createTitledBorder("Foto"));
        jLayeredPane2.setBounds(530, 20, 160, 150);
        jLayeredPane3.add(jLayeredPane2, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jDFecha_Nac.setBounds(140, 130, 180, 20);
        jLayeredPane3.add(jDFecha_Nac, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane6.setBorder(javax.swing.BorderFactory.createTitledBorder("Informacion de Familia"));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Nombre", "Apellido", "Doc. de Identificacion", "Sexo", "Fecha de Nacimiento", "Parentesco"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTable1);

        jScrollPane2.setBounds(10, 20, 452, 100);
        jLayeredPane6.add(jScrollPane2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton1.setText("Anadir");
        jButton1.setBounds(470, 30, 63, 23);
        jLayeredPane6.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane6.setBounds(510, 210, 550, 140);
        jLayeredPane3.add(jLayeredPane6, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLFoto.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jLFoto.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                Click_Foto(evt);
            }
        });
        jLFoto.setBounds(550, 40, 140, 120);
        jLayeredPane3.add(jLFoto, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setText("Segundo nombre: ");
        jLabel3.setBounds(270, 70, 100, 14);
        jLayeredPane3.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jTNombre_2.setBounds(380, 70, 110, 20);
        jLayeredPane3.add(jTNombre_2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel13.setText("Segundo apellido:");
        jLabel13.setBounds(270, 100, 120, 14);
        jLayeredPane3.add(jLabel13, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jTApellido_2.setBounds(380, 100, 110, 20);
        jLayeredPane3.add(jTApellido_2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel15.setText("Cedula de Identidad:");
        jLabel15.setBounds(10, 40, 140, 14);
        jLayeredPane3.add(jLabel15, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jTci.setBounds(140, 40, 170, 20);
        jLayeredPane3.add(jTci, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane3.setBounds(10, 120, 1050, 430);
        jLayeredPane1.add(jLayeredPane3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jBRegresar.setText("Regresar");
        jBRegresar.setBounds(990, 520, 80, 23);
        jLayeredPane1.add(jBRegresar, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jBAceptar.setText("Aceptar");
        jBAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAceptarActionPerformed(evt);
            }
        });
        jBAceptar.setBounds(900, 520, 71, 23);
        jLayeredPane1.add(jBAceptar, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jLayeredPane5.setBounds(30, 650, 110, 80);
        jLayeredPane1.add(jLayeredPane5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1087, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 552, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-1103)/2, (screenSize.height-590)/2, 1103, 590);
    }// </editor-fold>//GEN-END:initComponents

    private String cambio (String ruta)
    {
        String retorno = "";
        String arreglo_ruta[] = ruta.split("/");
        
        for (int i = 0; i< arreglo_ruta.length; i++)
        {
                if (i != arreglo_ruta.length -1 )  retorno = retorno + arreglo_ruta[i] + "\\";
                else retorno = retorno + arreglo_ruta[i];
        }
        return retorno;
    }
    
    private char sexo()
    {
        if (jRFemenino.isSelected()) return 'F';
        else if (jRMasculino.isSelected()) return 'M';    
        return 'x';
     }
    
    private void Click_Foto(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_Click_Foto
        
        int returnVal = fc.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) 
        {
            try {
                Image img = ImageIO.read(fc.getSelectedFile()); 
                direccion_foto = fc.getSelectedFile().getAbsolutePath();
                Image imagen_escalada = img.getScaledInstance(jLFoto.getWidth(), jLFoto.getHeight(),0);
                jLFoto.setIcon(new ImageIcon(imagen_escalada));
            } catch (IOException ex) {
                Logger.getLogger(Pagina_Creacion_PersonalW.class.getName()).log(Level.SEVERE, null, ex);
            }
        
        }
        
        
    }//GEN-LAST:event_Click_Foto

    private void jBAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAceptarActionPerformed

            if (PuenteConexion.InsertaPersonaBD(new Persona(Integer.parseInt(jTci.getText()),jTNombre_1.getText(),
            jTNombre_2.getText(), jTApellido_1.getText(),jTApellido_2.getText(),jDFecha_Nac.getCalendar().get
            (Calendar.DAY_OF_MONTH)+"-"+(jDFecha_Nac.getCalendar().get(Calendar.MONTH)+1)+"-"+jDFecha_Nac.
           getCalendar().get(Calendar.YEAR),sexo(),jTCorreo.getText(),jTTelefono_c.getText(),jTTelefono_h.getText(),
            jCEstado_Civil.getSelectedItem().toString(), jTDiscapacidad.getText(),cambio(direccion_foto),jTDireccion.getText()
            ))) 
                JOptionPane.showMessageDialog(null,"Persona agregada exitosamente","Confirmacion",JOptionPane.OK_OPTION);
            else JOptionPane.showMessageDialog(null,"Hubo un error en el momento de la insercion","Error",
                    JOptionPane.ERROR_MESSAGE);
    }//GEN-LAST:event_jBAceptarActionPerformed

    private void jRMasculinoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRMasculinoActionPerformed
        if (jRMasculino.isSelected()) jRFemenino.setSelected(false);
    }//GEN-LAST:event_jRMasculinoActionPerformed

    private void jRFemeninoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRFemeninoActionPerformed
       if (jRFemenino.isSelected()) jRMasculino.setSelected(false);
    }//GEN-LAST:event_jRFemeninoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(Pagina_Creacion_PersonalW.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Pagina_Creacion_PersonalW().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBAceptar;
    private javax.swing.JButton jBRegresar;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jCEstado_Civil;
    private com.toedter.calendar.JDateChooser jDFecha_Nac;
    private javax.swing.JLabel jLFoto;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLayeredPane jLayeredPane2;
    private javax.swing.JLayeredPane jLayeredPane3;
    private javax.swing.JLayeredPane jLayeredPane4;
    private javax.swing.JLayeredPane jLayeredPane5;
    private javax.swing.JLayeredPane jLayeredPane6;
    private javax.swing.JRadioButton jRFemenino;
    private javax.swing.JRadioButton jRMasculino;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTApellido_1;
    private javax.swing.JTextField jTApellido_2;
    private javax.swing.JTextField jTCorreo;
    private javax.swing.JTextArea jTDireccion;
    private javax.swing.JTextField jTDiscapacidad;
    private javax.swing.JTextField jTNombre_1;
    private javax.swing.JTextField jTNombre_2;
    private javax.swing.JTextField jTTelefono_c;
    private javax.swing.JTextField jTTelefono_h;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTci;
    // End of variables declaration//GEN-END:variables
}